/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import entity.RegisteredUser;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import mail.MailClient;
import manager.RegisteredUserManager;
import utils.FacesUtils;
import utils.HashGenerator;

/**
 *
 * @author Stefan
 */
@ManagedBean
@RequestScoped
public class RegistrationBean implements Serializable {

    protected String firstName;
    protected String lastName;
    protected String email;
    protected String password;
    protected String passwordConfirm;

    /** Creates a new instance of RegistrationBean */
    public RegistrationBean() {
    }

    public String register() {

        if (validatePasswordMatch() && validateUniqueEmail()) {
            //Validation successful
            RegisteredUserManager rum = new RegisteredUserManager();
            RegisteredUser ru = new RegisteredUser(firstName, lastName, email, HashGenerator.generateHash(password));
            rum.create(ru);
            MailClient.sendRegistrationMail(ru);
            return "verification_sent";
        }
        return null;
    }

    private boolean validatePasswordMatch() {
        if (password.equals(passwordConfirm)) {
            return true;
        } else {
            FacesUtils.throwFacesException("register:password_confirm", "Passwords don't match", FacesContext.getCurrentInstance());
            FacesUtils.throwFacesException("register:password", "Passwords don't match", FacesContext.getCurrentInstance());
            return false;
        }

    }

    private boolean validateUniqueEmail() {
        RegisteredUserManager rum = new RegisteredUserManager();
        RegisteredUser ru = rum.getRegisteredUserByMail(email);
        if (ru != null && ru.isVerified()) {
            FacesUtils.throwFacesException("register:username", "Email already taken", FacesContext.getCurrentInstance());
            return false;
        } else if (ru != null && !ru.isVerified()) {
            FacesUtils.throwFacesException(null, "Email exist but not verified , resending activation mail", FacesContext.getCurrentInstance());
            MailClient.sendRegistrationMail(ru);
            return false;
        } else {
            return true;
        }
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}
